#!/usr/bin/python

import MySQLdb
import os
import string
import sys
import re
import time
import zipfile
from datetime import datetime
import numpy
import matplotlib
import pylab



# check if the number of arguments is correct
if(len(sys.argv) != 5):
    print "Error:\tLess than 6 arguments provided"
    print "Usage:\tdatabase_loader.py host user password database"
    print "Example:\tdatabase_loader.py localhost slamusers slamusers slamusers"
    sys.exit(0)


# params to the script for loading
host = sys.argv[1]
user = sys.argv[2]
password = sys.argv[3]
database = sys.argv[4]

# dump the info just in case
print ""
print "Database Host: %s" % host
print "Database: %s" % database
print ""
print "Waiting 1 seconds before continuing..."
time.sleep(1);



# setup the database connection we're using
conn = MySQLdb.connect (host, user, password, database)
cursor = conn.cursor ()

# generate the query
lines = ('hlk81', 'hlk83', 'hlk84', 'hlk85', 'hulk', 'immatureB', 'immatureT', 'matureB', 'otg', 'shrek', 'srk31', 'srk32')

for line1 in lines:
    
    sql = "select chromosome, start_position, score from bed_data where line1 = '" + line1 + "' and line2 = 'input' order by chromosome, start_position"

    # perform the query
    try:
        cursor.execute(sql)        
    except Exception, e:
        print e
        print "Error executing SQL Statement: " + sql
    
    # fetch the results and create scores list
    results = cursor.fetchall()
    scores = []
    location = []
    for x in results:
        scores.append(x[2])
        location.append(x[0] + "-" + str(x[1]))
    
    print len(scores)
    #print scores
    filtered_scores = scores[abs(x) > 100]
    x2 = numpy.concatenate((filtered_scores, numpy.zeros(45000-44643)))
    x2 = x2.reshape((300,150))
    

print x2.shape
pylab.imshow(x2,interpolation=None)
pylab.hot()
#pylab.show()
pylab.savefig('HeatMap.pdf')
    
    # plot the scores
#    plot(scores)
#    xlabel('Sample #')
#    ylabel('Score')
#    xlim( -1000, 45000 )
#    ylim( -1000, 3000)
#    title("Distribution of Scores Across All Lines vs 'Input'")
#    grid(True)
#
#savefig('ScoreDistribution.pdf')
    #show()
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
